package com.wsd.smartcarwasher.controller;


import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.configuration.GetSessionConfig;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.Pagination;
import com.wsd.smartcarwasher.domain.Store;
import com.wsd.smartcarwasher.service.MachineService;
import com.wsd.smartcarwasher.service.StoreService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "（新增）门店相关")
@Controller
@CrossOrigin
public class StoreController {


    @Resource
    private WxService wxService;

    @Resource
    private HttpServletRequest request;
    @Resource
    private StoreService storeService;
    @Resource
    private MachineService machineService;
    @Log("查看商家对应的门店")
    @ApiOperation(value="查看商家对应的门店", notes = "Pc-查看商家对应的门店（partnerId=0为总平台查看，其他值为加盟商查看）", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "partnerId", value = "加盟商id", required = true, dataType = "Integer")
    @RequestMapping(value = "/v1/store_list", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap getPcStoreList(@RequestParam(value = "partnerId",defaultValue = "0") Integer partnerId,@RequestParam(value = "name",required = false) String name,@RequestParam Integer Page,@RequestParam Integer PageSize){
       if(name==null)
       {
           return ReturnUtil.Success(ResultState.QUERY_STORE_SUCCESS.getMsg(),new Pagination<Store>(Page,PageSize,storeService.getPartnerList(partnerId)));

       }
//        if(!(request.getSession().getAttribute("ispartnerId") ==null))
//        {
//            partnerId=Integer.parseInt(request.getSession().getAttribute("ispartnerId").toString());
//        }
        return ReturnUtil.Success(ResultState.QUERY_STORE_SUCCESS.getMsg(),new Pagination<Store>(Page,PageSize,storeService.getPartnerList(partnerId,name)));
    }


    @ApiOperation(value="商家端 查看商家对应的门店", notes = "商家端-查看商家对应的门店", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "partnerId", value = "加盟商id", required = true, dataType = "Integer")
    @RequestMapping(value = "/v1/store/list", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap getStoreStoreList(@RequestParam(value = "name",required = false) String name,@RequestParam Integer Page,@RequestParam Integer PageSize){
        GetSessionConfig config=new GetSessionConfig();
        Map<String,String> map=config.getStorePartnerId("查看商家对应的门店",request);
        int partnerId=Integer.parseInt(map.get("partnerId"));
        if(name==null)
        {
            return ReturnUtil.Success(ResultState.QUERY_STORE_SUCCESS.getMsg(),new Pagination<Store>(Page,PageSize,storeService.getPartnerList(partnerId)));

        }
        return ReturnUtil.Success(ResultState.QUERY_STORE_SUCCESS.getMsg(),new Pagination<Store>(Page,PageSize,storeService.getPartnerList(partnerId,name)));
    }

//
//    @Log("查询门店下的所属机器")
//    @ApiOperation(value="（新增）查询门店下的所属机器", notes = "查询门店对应机器", produces = "application/json", response = ModelMap.class)
//    @RequestMapping(value = "/v1/store/machine_list", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
//    @ResponseBody
//    public ModelMap getStoreMachine(@RequestParam Integer storeId,@RequestParam Integer Page,@RequestParam Integer PageSize){
//        if(storeId==0)
//        {
//            return ReturnUtil.Error("参数传递失败");
//        }
//        return ReturnUtil.Success(ResultState.QUERY_STORE_SUCCESS.getMsg(),new Pagination<Object>(Page,PageSize,machineService.queryStoreMachine(storeId)));
//    }

    @Log("新增门店")
    @ApiOperation(value="（新增）新建、修改门店", notes = "新建门店", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/store/machine_add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap addStoreMachine(@RequestBody Store store){

        if(store.getId()==null)
        {
            return ReturnUtil.Success(ResultState.ADD_STORE_SUCCESS.getMsg(),this.storeService.addStore(store));
        }else
        {
            return ReturnUtil.Success(ResultState.UPDATE_STORE_SUCCESS.getMsg(),this.storeService.updateStore(store));
        }
    }


    @ApiOperation(value="商家端  新建、修改门店", notes = "新建、修改门店", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/store/add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap updateStoreMachine(@RequestBody Store store){
        GetSessionConfig config=new GetSessionConfig();
        if(store.getId()==null)
        { Map<String,String> map= config.getStorePartnerId("新建",request);
            return ReturnUtil.Success(ResultState.ADD_STORE_SUCCESS.getMsg(),this.storeService.addStore(store));
        }else
        { Map<String,String> map= config.getStorePartnerId("修改门店",request);
            return ReturnUtil.Success(ResultState.UPDATE_STORE_SUCCESS.getMsg(),this.storeService.updateStore(store));
        }
    }


    @Log("删除门店")
    @ApiImplicitParam(name = "storeId", value = "门店id", required = true, dataType = "Integer")
    @ApiOperation(value="删除门店", notes = "新建门店", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/store/machine_delete", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap deleteStoreMachine(Integer storeId){


            return ReturnUtil.Success(ResultState.DELETE_STORE_SUCCESS.getMsg(),this.storeService.deleteStore(storeId));

    }


    @ApiImplicitParam(name = "storeId", value = "门店id", required = true, dataType = "Integer")
    @ApiOperation(value="商家端 删除门店", notes = "新建门店", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/store/delete", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap deleteStore(Integer storeId){

        GetSessionConfig config=new GetSessionConfig();
        Map<String,String> map= config.getStorePartnerId("删除门店",request);
        return ReturnUtil.Success(ResultState.DELETE_STORE_SUCCESS.getMsg(),this.storeService.deleteStore(storeId));

    }


    @ApiOperation(value="所有门店", notes = "所有门店", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/store/machine_all", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap allStoreMachine(){
        return this.storeService.allStore();
    }

}
